package com.person.jc.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.person.jc.utils.CreateCode;


/*****************************************************************************
 * @程序名: UploadLicense.java
 * @日期 : Feb 20, 2012
 * @作者 : 武智雄 (491933243@qq.com)
 * @模块 : [项目]-[一级菜单]-[二级菜单]-[上传图片或文件] 
 * @描述 : [功能简介] 
 * ------------------------------------------------------------ 
 * 修改历史 
 * 序号             日期                      修改人                  修改原因 
 * 1
 * 2
 ******************************************************************************/
public class UploadImg extends HttpServlet {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
        String savePath = this.getServletConfig().getServletContext().getRealPath("");
        String folderName = request.getParameter("folder");
        File f1 = new File(savePath+"/static/UserFiles/"+folderName+"/");
        if (!f1.exists()) {
            f1.mkdirs();
        }
        
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setHeaderEncoding("utf-8");
        List<FileItem> fileList = null;
        try {
            fileList = upload.parseRequest(request);
        } catch (FileUploadException ex) {
            return ;
        }
        Iterator<FileItem> it = fileList.iterator();
        String name = "";
        String extName = "";
        Boolean ifFirst = true;

        while (it.hasNext()) {
            FileItem item = it.next();
            if (!item.isFormField()) {
                name = item.getName();
                @SuppressWarnings("unused")
				long size = item.getSize();
                @SuppressWarnings("unused")
                String type = item.getContentType();
                if (name == null || name.trim().equals("")) {
                    continue;
                }
                //扩展名格式： 
                if (name.lastIndexOf(".") >= 0) {
                    extName = name.substring(name.lastIndexOf("."));
                }
                name = "/static/UserFiles/"+folderName+"/"+CreateCode.generateKey()+extName;
                if(ifFirst){                    
                    ifFirst = false;
                }
                File saveFile = new File(savePath + name);
                try {
                    item.write(saveFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        response.getWriter().print(name);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doGet(request,response);
    }
}